//
//  OrderManagementViewController.m
//  KindergartenDemo
//
//  Created by 李传铎 on 2020/11/7.
//  Copyright © 2020 李传铎. All rights reserved.
//

#import "OrderManagementViewController.h"
#import "LCDMultiFunctionSegmentedView.h"
#import "SpellGroupManagementListViewController.h"

@interface OrderManagementViewController ()<UIScrollViewDelegate>
@property (nonatomic ,strong) LCDMultiFunctionSegmentedView *spellSegmentedView;
@property (nonatomic ,strong) UIScrollView *scrollView;

@end

@implementation OrderManagementViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatUI];
}
- (void)creatUI{
   
    _spellSegmentedView = [[LCDMultiFunctionSegmentedView alloc]initWithOriginY:CGRectMake(0, __kNavigationBarHeight__, SCREEN_WIDTH, 40) left:15 right:15 headTitle:@"" headTitleFont:fontCount(15) headTitleColor:@"333333" Titles:@[@"全部",@"待支付",@"待使用",@"已完成"] titleFont:fontCount(16) titleColor:@"333333" titleSelectedColor:@"333333" isUniform:YES maxUniformCount:4 isVerticalLine:NO verticallineColor:@"#F65758" verticallineHeight:10 isUnderline:YES underlineColor:@"F65758" underlineWidth:30 underlineHeight:3];
    [self.view addSubview:_spellSegmentedView];

    WEAKSELF;
    _spellSegmentedView.segmentClock = ^(NSInteger index){
        STRONGSELF;
        [UIView animateWithDuration:0.5 animations:^{
            CGFloat offsetX =  SCREEN_WIDTH * index;
            CGFloat offsetY = strongSelf.scrollView.contentOffset.y;
            CGPoint offset = CGPointMake(offsetX, offsetY);
            [strongSelf.scrollView setContentOffset:offset animated:YES];
        }];
    };
    [self.view addSubview:self.scrollView];
   
    for (int i = 0; i < 4; i++) {
        SpellGroupManagementListViewController * NewBnakChildVC = [[SpellGroupManagementListViewController alloc]init];;
        [self addChildViewController:NewBnakChildVC];
    }
//    NSInteger count = 0;
//    if (!IsEmpty(_creditType)) {
//        count = [self getArrIndex:_dataArr] ;
//        [_LoanSegmentedView  changeSegmentedControlWithIndex:count];
//        CGFloat offsetX =  SCREEN_WIDTH * count;
//        CGFloat offsetY = self.scrollView.contentOffset.y;
//        CGPoint offset = CGPointMake(offsetX, offsetY);
//        [self.scrollView setContentOffset:offset animated:YES];
//    }
    
    SpellGroupManagementListViewController *vc = self.childViewControllers[0];
    vc.view.frame = CGRectMake(0 *self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    [self.scrollView addSubview:vc.view];
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH *4, 0);
}
/** 滚动结束后调用（代码导致） */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 获得索引
    NSUInteger index = scrollView.contentOffset.x / self.scrollView.frame.size.width;
    
    [_spellSegmentedView changeSegmentedControlWithIndex:index];
   
    SpellGroupManagementListViewController *newsVc = self.childViewControllers[index];
    
    if (newsVc.view.superview) return;
    newsVc.view.frame = CGRectMake(index*SCREEN_WIDTH, 0, SCREEN_WIDTH, scrollView.frame.size.height);
    [self.scrollView addSubview:newsVc.view];
    
    
    
}
/** 滚动结束（手势导致） */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

/** 正在滚动 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, __kNavigationBarHeight__ + 40, SCREEN_WIDTH, SCREEN_HEIGHT - __kNavigationBarHeight__ - 40)];
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.delegate = self;
        self.scrollView.bounces = NO;
//        self.scrollView.scrollEnabled = NO;
        self.scrollView.pagingEnabled = YES;
        self.scrollView.contentOffset = CGPointZero;
        
    }
    return _scrollView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
